from apps import db


class Publish(db.Model):
    __tablename__ = 'tb_publish'
    p_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='序号')
    name = db.Column(db.String(32), comment='出版社名称')
    address = db.Column(db.String(64), comment='出版社地址')
    intro = db.Column(db.Text, comment='出版社简介', nullable=True)
    books = db.relationship('Book', backref='back_publish', lazy=True, passive_deletes=True)


a_b = db.Table('author_book',
               db.Column('b_id', db.Integer, db.ForeignKey('tb_book.b_id'), primary_key=True),
               db.Column('a_id', db.Integer, db.ForeignKey('tb_author.a_id'), primary_key=True))

class Author(db.Model):
    __tablename__ = 'tb_author'
    a_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='序号')
    name = db.Column(db.String(20), comment='作者名称')
    age = db.Column(db.Integer, comment='作者年龄')
    mobile = db.Column(db.String(11), comment='作者手机号')
    address = db.Column(db.String(64), comment='作者地址')
    intro= db.Column(db.Text, comment='作者简介', nullable=True)
    books = db.relationship('Book',secondary="author_book", backref='back_author')


class Book(db.Model):
    __tablename__ = 'tb_book'
    b_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='序号')
    name = db.Column(db.String(32), comment='书名')
    isbn = db.Column(db.String(50), comment='ISBN编号')
    photo = db.Column(db.String(128), comment='图片封面')
    price = db.Column(db.Numeric, comment='价格')
    pub_id = db.Column(db.Integer, db.ForeignKey('tb_publish.p_id'))


class User(db.Model):
    __tablename__ = 'tb_user'
    u_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='序号')
    username = db.Column(db.String(20), comment='用户名')
    password = db.Column(db.String(20), comment='密码')
    email = db.Column(db.String(50), comment='邮箱')
